﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.DataControls;
using CarbonExchange.Bll;

public partial class Admin_Questions : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ds.Select += new EventHandler<EventDrivenDataSourceSelectEventArgs>(ds_Select);
        ds.Insert += new EventHandler<EventDrivenDataSourcePerformOperationEventArgs>(ds_Insert);
        ds.Update += new EventHandler<EventDrivenDataSourcePerformOperationEventArgs>(ds_Update);
        ds.Delete += new EventHandler<EventDrivenDataSourcePerformOperationEventArgs>(ds_Delete);
        dsSections.Select += new EventHandler<EventDrivenDataSourceSelectEventArgs>(dsSections_Select);
        dsType.Select += new EventHandler<EventDrivenDataSourceSelectEventArgs>(dsType_Select);
        btnUpload.Click += new EventHandler(btnUpload_Click);
    }

    void btnUpload_Click(object sender, EventArgs e)
    {
        CarbonExchange.Bll.QuestionFactory factory = new CarbonExchange.Bll.QuestionFactory();
        var sections = new SectionFactory().GetAll().ToList();
        var enumsNamesIncluded = factory.GetAll().Select(x => (int)x.QuestionType);
        var newQuestionsEnums = Enum.GetValues(typeof(QuestionTextConfiguration.QuestionTypes)).AsQueryable().Cast<int>().Where(x => x != 0 && !enumsNamesIncluded.Contains(x)).Select(x => (QuestionTextConfiguration.QuestionTypes)x);
        var newQuestions = newQuestionsEnums.Select(x => new Question(){Name = GetNameFromEnum(x), QuestionType = x, SectionId = GetSectionIdFromEnum((int)x, sections)}).ToList();
        factory.Save(newQuestions);
        grid.Rebind();
    }

    private int GetSectionIdFromEnum(int type, List<Section> sections)
    {
        /// Energy:100-199
        /// Transport:200-299
        /// Waste:300-399
        /// Water:400-499
        /// Shopping:500-599
        Section tmp = null;
        if (type >= 100 && type < 200)
            tmp = sections.Where(x => x.Name.Equals("Energy", StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault();
        else if(type >= 200 && type < 300)
            tmp = sections.Where(x => x.Name.Equals("Transport", StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault();
        else if (type >= 300 && type < 400)
            tmp = sections.Where(x => x.Name.Equals("Waste", StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault();
        else if (type >= 400 && type < 500)
            tmp = sections.Where(x => x.Name.Equals("Water", StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault();
        else if (type >= 500 && type < 600)
            tmp = sections.Where(x => x.Name.Equals("Shopping", StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault();
        return tmp != null ? tmp.Id : 0;
    }

    private string GetNameFromEnum(QuestionTextConfiguration.QuestionTypes type)
    {
        var tmp = type.ToString().ToCharArray().Select((x, i) => (i != 0 && Char.IsUpper(x)) ? string.Concat(" ", x) : x.ToString());
        return string.Join("", tmp.ToArray());
    }

    void dsType_Select(object sender, EventDrivenDataSourceSelectEventArgs e)
    {
        e.SetData(Enum.GetNames(typeof(CarbonExchange.Bll.QuestionTextConfiguration.QuestionTypes)).Where(x => x != CarbonExchange.Bll.QuestionTextConfiguration.QuestionTypes.Unknown.ToString()).OrderBy(x => x).Select(x => new { Name = x }));
    }

    void dsSections_Select(object sender, EventDrivenDataSourceSelectEventArgs e)
    {
        CarbonExchange.Bll.SectionFactory factory = new CarbonExchange.Bll.SectionFactory();
        e.SetData(factory.GetAll());
    }

    void ds_Delete(object sender, EventDrivenDataSourcePerformOperationEventArgs e)
    {
        int id = (int)e.Keys["Id"];
        CarbonExchange.Bll.QuestionFactory factory = new CarbonExchange.Bll.QuestionFactory();
        CarbonExchange.Bll.Question item = factory.GetById(id);
        if (item != null)
        {
            factory.Delete(item);
            e.RowsChanged = 1;
        }
    }

    void ds_Update(object sender, EventDrivenDataSourcePerformOperationEventArgs e)
    {
        if (IsValid)
        {
            int id = (int)e.Keys["Id"];
            CarbonExchange.Bll.QuestionFactory factory = new CarbonExchange.Bll.QuestionFactory();
            CarbonExchange.Bll.Question item = factory.GetById(id);
            if (item != null)
            {
                item.Name = e.NewValues["Name"] as string;
                item.QuestionType = (CarbonExchange.Bll.QuestionTextConfiguration.QuestionTypes)Enum.Parse(typeof(CarbonExchange.Bll.QuestionTextConfiguration.QuestionTypes), e.NewValues["QuestionType"] as string);
                item.SectionId = int.Parse((string)e.NewValues["SectionId"]);
                factory.Save(item);
                e.RowsChanged = 1;
            }

        }
    }

    void ds_Insert(object sender, EventDrivenDataSourcePerformOperationEventArgs e)
    {
        if (IsValid)
        {
            CarbonExchange.Bll.QuestionFactory factory = new CarbonExchange.Bll.QuestionFactory();
            CarbonExchange.Bll.Question item = new CarbonExchange.Bll.Question();
            item.Name = (string)e.NewValues["Name"];
            item.QuestionType = (CarbonExchange.Bll.QuestionTextConfiguration.QuestionTypes)Enum.Parse(typeof(CarbonExchange.Bll.QuestionTextConfiguration.QuestionTypes), e.NewValues["QuestionType"] as string);
            item.SectionId = int.Parse((string)e.NewValues["SectionId"]);
            factory.Save(item);
            e.RowsChanged = 1;
        }
    }

    void ds_Select(object sender, EventDrivenDataSourceSelectEventArgs e)
    {
        CarbonExchange.Bll.QuestionFactory factory = new CarbonExchange.Bll.QuestionFactory();
        e.SetData(factory.GetAll());
    }
}
